<template>
  <div class="content-box">
    <div class="header">
      <el-button type="danger" @click="deleteSelect">删除选中</el-button>
      <el-button type="primary" @click="toggleSelection()">取消选中</el-button>
    </div>
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="220" />
      <el-table-column label="日期">
        <template slot-scope="{ row }">
          {{ row.createTime | parseTime("{y}-{m}-{d} {h}:{i}") }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名">
        <template slot-scope="{ row }">
          <img :src="row.url" style="width: 50px; height: 50px; object-fit: cover" @click="showDetailPhotos(row.url)">
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="照片预览" :visible.sync="dialogVisible" width="1200px">
      <img :src="imgUrlNow" style="width: 1000px; object-fit: cover">
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="dialogVisible = false"
        >确 定</el-button>
      </span>
    </el-dialog>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryList.currentPage"
      :limit.sync="queryList.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { deletePhotos, photosList } from '@/api/doubleEleven'
export default {
  components: { Pagination },
  data() {
    return {
      total: 0,
      queryList: {
        currentPage: 1,
        pageSize: 10
      },
      tableData: [],
      dialogVisible: false,
      imgUrlNow: '',
      multipleSelection: []
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      photosList(this.queryList).then(response => {
        const { total, records } = response.data
        this.total = total
        this.tableData = records
      })
    },
    showDetailPhotos(url) {
      this.dialogVisible = true
      this.imgUrlNow = url
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteSelect() {
      console.log(this.multipleSelection)
      let ids = []
      ids = this.multipleSelection.map(item => {
        return item.id
      })
      ids = ids.join(',')
      deletePhotos({ ids }).then(response => {
        if (response.code === '200') {
          this.$message.success('删除成功')
          this.dialogVisible = false
          this.getList()
        }
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.content-box {
  padding: 20px;
  .header{
      margin-bottom: 15px;
  }
}
</style>
